
//
//  UIImage+Extention.swift
//  Weibo
//
//  Created by 李学林 on 15/12/13.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit


extension UIImage {

    //截屏方法
    class func getScreenShot()->UIImage {
        
        let window = UIApplication.sharedApplication().keyWindow
        
        UIGraphicsBeginImageContext(window!.size)
        
        window?.drawViewHierarchyInRect((window?.bounds)!, afterScreenUpdates: false)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return image
    }

    
    func scaleToWidth(width:CGFloat)->UIImage{
        
        if size.width < width{
            return self
        }
        
        let height = width/size.width * size.height
        
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        // 1.开启上下文
        UIGraphicsBeginImageContext(rect.size)
        // 2.将图片绘制到上下文
        drawInRect(rect)
        // 3.从上下文中获取图片
        let result = UIGraphicsGetImageFromCurrentImageContext()
        // 4.关闭上下文
        UIGraphicsEndImageContext()
        // 5.返回
        return result
    }
    
}


